<?php
if(!check_manager_login()) exit('无权查看，请登录!');
//require_once(MODEL_PATH.DS.'Attribute.php');
require_once(MODEL_PATH.DS.'AttributeValue.php');
require_once(MODEL_PATH.DS.'Category.php');
require_once(MODEL_PATH.DS.'Brand.php');
require_once(MODEL_PATH.DS.'Product.php');
require_once(MODEL_PATH.DS.'ProductDesc.php');
require_once(MODEL_PATH.DS.'ProductAttribute.php');
require_once(MODEL_PATH.DS.'ProductCategory.php');
require_once(MODEL_PATH.DS.'ProductTag.php');
require_once(MODEL_PATH.DS.'ProductImage.php');
require_once(LIB_PATH.DS.'upload.class.php');
require_once(LIB_PATH.DS.'thumbnail.class.php');
$Brand = new Brand();
$Category = new Category();
$AttributeValue = new AttributeValue();

if(check_post()){
	$name = trim($_POST['product_name']);
	$manufacturer_id = trim($_POST['manufacturer_id']);
	$category_id = $_POST['category_id'];
	$attribute_value_id = $_POST['attribute_value_id'];
	$meta_description = trim($_POST['meta_description']);
	$meta_keyword = trim($_POST['meta_keyword']);
	$description = trim($_POST['description']);
	$product_tag = trim($_POST['product_tag']);
	$model = trim($_POST['model']);
	$price = trim($_POST['price']);
	$discount_price = trim($_POST['discount_price']);
	$quantity = trim($_POST['quantity']);
	$minimum = trim($_POST['minimum']);
	$subtract = trim($_POST['subtract']);
	$stock_status_id = trim($_POST['stock_status_id']);
	$shipping = trim($_POST['shipping']);
	$location = trim($_POST['location']);
	$product_weight = trim($_POST['product_weight']);
	$product_length = trim($_POST['product_length']);
	$product_width = trim($_POST['product_width']);
	$product_height = trim($_POST['product_height']);
	//$product_image = trim($_POST['product_image']);
	$date_available = trim($_POST['date_available']);
	$status = trim($_POST['status']);
	$is_best = trim(@$_POST['is_best'])?1:0;
	$is_new = trim(@$_POST['is_new'])?1:0;
	$is_hot = trim(@$_POST['is_hot'])?1:0;
	$sort_order = trim($_POST['sort_order']);
	if(empty($name)||empty($model)){
		redirect('index.php?mod=product&act=add','产品名称或模型不能为空！');
	}
	$Upload = new Upload('product_images/');
	$Upload->setMaxSize(1024000);
	$upload_status = $Upload->move();
	$messages = $Upload->getMessages();

	$Product = new Product();
	
	$image_name = $Upload->getFilename();
	
	$Thumbnail = new Thumbnail(PRODUCT_IMAGES.$image_name[0]);
	$Thumbnail->setDestination(PRODUCT_IMAGES);
	$Thumbnail->create();
	$thb_name = $Thumbnail->getFilename();
	$now = date('Y-m-d H:i:s');
	$data = array(
		'model'	=> $model,
		'location'=>$location,
		'quantity'=>$quantity,
		'product_img'=>$image_name[0],
		'product_thumb'=>$thb_name,
		'manufacturer_id'=>$manufacturer_id,
		'shipping'=>$shipping,
		'price'=>$price,
		'discount_price'=>$discount_price,
		'date_available'=>$date_available,
		'weight' =>$product_weight,
		'length'=>$product_length,
		'width'=>$product_width,
		'height'=>$product_height,
		'status'=>$status,
		'date_added'=>$now,
		'date_modified'=>$now,
		'sort_order'=>$sort_order,
		'subtract'=>$subtract,
		'minimum'=>$minimum,
		'is_best'=>$is_best,
		'is_new'=>$is_new,
		'is_hot'=>$is_hot,
		'panel'=>trim($_POST['panel']),
		'side'=>$_POST['side'],
		'neck'=>$_POST['neck'],
		'bridge'=>$_POST['bridge'],
		'chordbutton'=>$_POST['chordbutton'],
		'srbicularis'=>$_POST['srbicularis'],
		'saddle'=>$_POST['saddle'],
		'strings'=>$_POST['strings'],
		'chord'=>$_POST['chord'],
		'color'=>$_POST['color'],
	);
	$product_id = $Product->addProduct($data);
	if(!$product_id){
		redirect('index.php?mod=product&act=add','添加产品信息失败！');
	}
	$ProductDesc = new ProductDesc();
	$data = array(
		'product_id'=>$product_id,
		'name'=>$name,
		'meta_keywords'=>$meta_keyword,
		'meta_description'=>$meta_description,
		'description'=>$description
	);
	$pd_id = $ProductDesc->addProductDesc($data);
	if(!$pd_id){
		redirect('index.php?mod=product&act=add','添加产品描述信息失败！');
	}
	$ProductCategory = new ProductCategory();
	if($category_id){
		foreach($category_id as $cid){
			$data = array(
				'product_id'=>$product_id,
				'category_id'=>$cid,
			);
			$pc_id = $ProductCategory->addPC($data);
		}
	}
	$ProductAttribute = new ProductAttribute();
	if($attribute_value_id){
		foreach($attribute_value_id as $av_id){
			$data = array(
				'product_id'=>$product_id,
				'attribute_value_id'=>$av_id,
			);
			$pa_id = $ProductAttribute->addPA($data);
		}
	}
	$ProductTag = new ProductTag();
	$tag_arr = explode(",",$product_tag);
	foreach($tag_arr as $tag){
		 $result = $ProductTag->addTag($product_id,$tag);
	}
	if($result) redirect('index.php?mod=product&act=add','添加产品成功！');

}

$brand_list = $Brand->getBrandList();
$brand_select = create_select($brand_list,'manufacturer_id','name');
$cat_select = $Category->getCatSelect();
$attr_list = $AttributeValue->getAllAttributeValue();
$attr_select = '';
foreach($attr_list as $list) {
	//$select = (trim($list['']) == $default_val)? ' selected': '';
	$attr_select .= '<option value="'. trim($list['attribute_value_id']) .'">'. trim($list['name']).':'.trim($list['value']) ."</option>";
}
include load_tpl('product_add');
?>